package com.netbao.testeasypoi;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.pro.mapper.WayBillMapper;
import com.netbao.pro.model.WayBill;
import com.netbao.pro.model.WayBillExample;
import com.netbao.pro.vomapper.WayBillVoMapper;
import com.netbao.sys.utils.ExcelUtil;
import com.netbao.sys.utils.ShiroUtils;
import com.netbao.sys.vomapper.PersonMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhouxp
 * @create 2019-08-11 10:05
 */
@RestController
public class EasyPoiTestController {
    /*	@Resource
        private PersonMapper personMapper;*/
    @Resource
    PersonMapper personMapper;

    @Resource
    private WayBillMapper wayBillMapper;

    @Resource
    private WayBillVoMapper wayBillVoMapper;

    //@RequiresPermissions("export")
    @RequestMapping("export")
    public void export(HttpServletResponse response) {

        //模拟从数据库获取需要导出的数据
//        List<Person> personList = new ArrayList<>();
//        Person person1 = new Person("1", "路飞", 1, new Date(), 1);
//        Person person2 = new Person("2", "娜美", 2, addDays(new Date(), 3), 1);
//        Person person3 = new Person("3", "索隆", 1, addDays(new Date(), 10), 1);
//        Person person4 = new Person("4", "小狸猫", 1, addDays(new Date(), -10), 1);
//        personList.add(person1);
//        personList.add(person2);
//        personList.add(person3);
//        personList.add(person4);

        List<Person> geterror = personMapper.geterror();
//        System.out.println(geterror);
        //导出操作
        ExcelUtil.exportExcel(geterror, "花名册", "草帽一伙", Person.class, "海贼王.xls", response);
    }

    @PostMapping("excelImport")
    public PageResult excelImport(@RequestParam("file") MultipartFile file) {
//        List<Person> personList = null;
//        personList = ExcelUtil.importExcel(file, 1, 1, Person.class);
/*		for (Person p:personList){
			personMapper.insert(p);
		}*/
        //personMapper.insertBatch(personList);
//        personMapper.delete();
//        wayBillVoMapper.delete();
//        personMapper.insertBatch(personList);
//        wayBillVoMapper.insertBatch(wayBills);
//        Map<String, Object> result = new HashMap<>();
//        personMapper.check(result);
//        Integer r = (Integer) result.get("reuslt");
//        System.out.println(r);
//        System.out.println(personList);
        boolean flag=false;
        List<WayBill> wayBills = ExcelUtil.importExcel(file, 1, 1, WayBill.class);
        ArrayList<WayBill> erro = new ArrayList<>();
        for (WayBill wayBill : wayBills) {
            flag=false;
            Date stime = wayBill.getStime();
            Date etime = wayBill.getEtime();
            int min = (int) (( wayBill.getEtime().getTime() - wayBill.getStime().getTime()) / (1000*60));
            if(min<20){
                flag=true;
                erro.add(wayBill);
                continue;
            }
            WayBillExample wayBillExample = new WayBillExample();
            wayBillExample.createCriteria().andIdcardEqualTo(wayBill.getIdcard());
            List<WayBill> wayBills1 = wayBillMapper.selectByExample(wayBillExample);
            for (WayBill bill : wayBills1) {
                Date stime1 = bill.getStime();
                Date etime1 = bill.getEtime();
                if((stime.after(stime1)&&stime.before(etime1))||
                        (etime.after(stime1) &&etime.before(etime1)) ||
                        (stime.before(stime1)&&etime.after(etime1))
                ){
                    flag=true;
                    erro.add(bill);
                    break;
                }
            }
            if(flag==false) {
                wayBill.setIssigned(true);
                wayBill.setChecktime(new Date());
                wayBill.setCstatus((long) 1);
                wayBill.setEid(ShiroUtils.getUserEntity().getEid().longValue());
                wayBill.setOpid(ShiroUtils.getUserId().toString());
                wayBill.setOpstatus(true);
                wayBillMapper.insert(wayBill);
            }
        }
        PageBean pageBean = new PageBean();
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageInfo<WayBill> wayBillPageInfo = new PageInfo<WayBill>(erro);
        return new PageResult(wayBillPageInfo.getTotal(),wayBillPageInfo.getList());
    }
}
